package com.akeil.flash.preferences;

import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

import flash.Activator;

public class DirectoryPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

	private ScopedPreferenceStore preferences;
	
	private DirectoryFieldEditor outputWordDirectoryFieldEditor;
	private DirectoryFieldEditor outputXMLDirectoryFieldEditor;
	private DirectoryFieldEditor otherDirectoryFieldEditor;

	public DirectoryPreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void performDefaults() {
		outputWordDirectoryFieldEditor.loadDefault();
		outputXMLDirectoryFieldEditor.loadDefault();
		otherDirectoryFieldEditor.loadDefault();
	}

	@Override
	public boolean performOk() {
		return super.performOk();
	}

	@Override
	protected void performApply() {
		IPreferenceStore preferenceStore = getPreferenceStore();
		preferenceStore.setValue(IPreferenceConstants.DIRECTORY_OUTPUT_WORD, outputWordDirectoryFieldEditor.getPreferenceName());
		preferenceStore.setValue(IPreferenceConstants.DIRECTORY_OUTPUT_XML, outputXMLDirectoryFieldEditor.getPreferenceName());
		preferenceStore.setValue(IPreferenceConstants.DIRECTORY_OTHER, otherDirectoryFieldEditor.getPreferenceName());
	}

	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void createFieldEditors() {
		// TODO Auto-generated method stub
		Composite parent = getFieldEditorParent();

		GridLayout gl = (GridLayout) parent.getLayout();
		gl.marginWidth = gl.marginHeight = gl.verticalSpacing = 10;
		// 重新布局
		outputWordDirectoryFieldEditor = new DirectoryFieldEditor(IPreferenceConstants.DIRECTORY_OUTPUT_WORD, "试验报告(Word)输出路径", getFieldEditorParent());
		outputXMLDirectoryFieldEditor = new DirectoryFieldEditor(IPreferenceConstants.DIRECTORY_OUTPUT_XML, "试验报告(XML)输出路径", getFieldEditorParent());
		otherDirectoryFieldEditor = new DirectoryFieldEditor(IPreferenceConstants.DIRECTORY_OTHER, "其他文件数据路径", getFieldEditorParent());
		addField(outputWordDirectoryFieldEditor);
		addField(outputXMLDirectoryFieldEditor);
		addField(otherDirectoryFieldEditor);
	}

}
